< previous page page_60 next page >

Page 60
For example, the C code syntax for the GetModuleHandle API function is:
e10a6a0a59f035e870d90cca05b85fce.gif
HMODULE GetModuleHandle(lpszModuleName)

LPCSTR lpszModuleName;      /* The name of the module */
This function takes as a parameter an LPCSTR holding the name of a module. LPCSTR is a fancy name for a C stringan array of characters that ends with an ASCII 0 (NULL) character. To translate a Visual Basic string into an LPCSTR, you use the ByVal keyword. The Visual Basic declaration for the GetModuleHandle function is:
e10a6a0a59f035e870d90cca05b85fce.gif
Private Declare Function GetModuleHandle Lib "kernel32" _
    Alias "GetModuleHandleA" (ByVal lpModuleName As String) _
    As Long
NULL Values
Strings have one more important special case. If you declare a string variable and pass it into an API routine without initializing it, the string is passed as NULL rather than as an empty string. When the string is passed as NULL, the API routine receives 0 as the address of the character data. When it receives an empty string, the function receives an address that points to a string that contains only the ASCII value 0. The difference is small but important.
Many API routines understand the value NULL and take special action when they see it. The CreateCaret function, for example, takes as one of its parameters the bitmap it should use to create a new system caret. If this parameter is NULL, the function creates a solid caret.
While you can use an uninitialized string to pass NULL to an API routine, this could be very confusing. The API routine call will not indicate that the string is uninitialized, and if you later accidentally give the string a value, the routine may no longer work properly.
Better methods for passing NULL to an API routine include passing the value ByVal 0&, the value ByVal Nothing, or the constant vbNullString.
API Functions That Modify Strings
Some API routines modify the contents of the string you pass them. The API routine will not allocate more space for the string, however, so you must make sure the string is large enough to hold all of the function's output before you call the function. One way to do this is to give the string a fixed size in its declaration.
e10a6a0a59f035e870d90cca05b85fce.gif
Dim txt As String * 256
This line of code makes the string 256 characters long, which is long enough for most API routines.
Another method for sizing a string is to fill it with some characters using the String command. The following code fills the variable txt with 256 ASCII 0 (NULL) characters.
e10a6a0a59f035e870d90cca05b85fce.gif
Dim txt As String 
    txt = String(256, 0)

 
< previous page page_60 next page >